SQL with R

This will actually be a brief lecture, because connecting R to a SQL database is completely dependent on the type of database you are using (MYSQL, Oracle, etc...).

So instead of trying to cover all of these (since each requires a different package), we'll use this lecture to point you in the right direction for various database types, once you've downloaded the correct library, actually connecting is usually quite simple, then its just a matter of passing in SQL queries through R.

We'll show a general version using the DBI package, then point to more specific resources.

RODBC - General Use

The RODBC library is one way of connecting to databases. Regardless of what you decide to use, I highly recommend a google search consisting of your database of choice + R. Here's an example use of RODBC

In [ ]:
install.packages("RODBC") 
# RODBC Example of syntax
library(RODBC)

myconn <-odbcConnect("Database_Name", uid="User_ID", pwd="password")
dat <- sqlFetch(myconn, "Table_Name")
querydat <- sqlQuery(myconn, "SELECT * FROM table")
close(myconn)

MySQL

The RMySQL package provides an interface to MySQL.

Oracle

The ROracle package provides an interface for Oracle.

JDBC

The RJDBC package provides access to databases through a JDBC interface.

Again, Google is the best way to go for your personal situation since databases and your permissions can differ a lot!